<?php

namespace App\Store\Controllers\Sysset\Commission;

use App\Common\Services\Store\Common as StoreCommon;
use App\Store\Models\Basic\GoodsCategory;
use App\Store\Models\Sysset\Commission\Recovery;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Store\Validate\Sysset\Commission\Recovery as ValidateRecovery;

/**
 * 纯回收
 */
class RecoveryController extends Controller
{
    /**
     * 新增
     */
    public function dopostadd(Request $request)
    {
        $req_data = $request->only(['store_ids', 'goods_category_names', 'money', 'gram_min', 'gram_max']);
        $Validate = new ValidateRecovery('add');
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        $store_ids = $req_data['store_ids'];
        $store_ids = is_array($store_ids) ? $store_ids : (is_array(json_decode($store_ids, true)) ? json_decode($store_ids, true) : []);

        $goods_category_names = $req_data['goods_category_names'];
        $goods_category_names = is_array($goods_category_names) ? $goods_category_names : (is_array(json_decode($goods_category_names, true)) ? json_decode($goods_category_names, true) : []);

        // 组装数据
        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $cp_id = $accountinfo['cp_id'];
        $recovery = [];
        foreach ($store_ids as $store_id) {

            foreach ($goods_category_names as $k => $goods_category_name) {
                $one = GoodsCategory::where([
                    'store_id' => $store_id,
                    'category_name' => $goods_category_name,
                    'is_del'=>1
                ])->first();
                if (!empty($one)) {
                    $one = $one->toArray();
                    $goods_category_id = $one['id'];

                    // Check if the combination of store_id and goods_category_id already exists
                    $existing_discount = Recovery::where([
                        'cp_id' => $cp_id,
                        'store_id' => $store_id,
                        'goods_category_id' => $goods_category_id,
                        'is_del' => 1
                    ])->first();

                    if ($existing_discount) {
                        return response()->json(['code' => '001', 'msg' => '商品类别 ' . $goods_category_name . ' 的配置已存在']);
                    }
                    $recovery[$k]['cp_id'] = $cp_id;
                    $recovery[$k]['store_id'] = $store_id;
                    $recovery[$k]['goods_category_id'] = $goods_category_id;
                    $recovery[$k]['money'] = !empty($req_data['money']) ? $req_data['money'] : 0;
                    $recovery[$k]['gram_min'] = !empty($req_data['gram_min']) ? $req_data['gram_min'] : 0;
                    $recovery[$k]['gram_max'] = !empty($req_data['gram_max']) ? $req_data['gram_max'] : 0;
                    $recovery[$k]['create_account_id'] = $accountinfo['id'];
                    $recovery[$k]['create_account_real_name'] = $accountinfo['real_name'];
                    $recovery[$k]['create_at'] = time();
                }
            }

            if ($recovery) Recovery::insert($recovery);
        }
        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }

    /**
     * 列表
     */
    public function getlist(Request $request)
    {
        $list = Recovery::getList($request);
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $list]);
    }

    /**
     * 详情
     */
    public function getone(Request $request)
    {
        $one = Recovery::where('id', $request['id'])->first();
        if ($one) {
            $one = $one->toArray();
            return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $one]);
        }
        return response()->json(['code' => '000', 'msg' => '获取失败']);
    }

    /**
     * 编辑
     */
    public function dopostedit(Request $request)
    {
        $req_data = $request->only(['store_id', 'goods_category_id', 'discount_min', 'discount_max', 'commission_rate', 'commission_rate_1', 'commission_rate_2', 'gram_min', 'gram_max', 'money']);
        $Validate = new ValidateRecovery('edit');
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $req_data['money'] = !empty($req_data['money']) ? $req_data['money'] : 0;
        $req_data['gram_min'] = !empty($req_data['gram_min']) ? $req_data['gram_min'] : 0;
        $req_data['gram_max'] = !empty($req_data['gram_max']) ? $req_data['gram_max'] : 0;
        $req_data['update_account_id']        = $accountinfo['id'];
        $req_data['update_account_real_name'] = $accountinfo['real_name'];
        $req_data['update_at']                = time();

        Recovery::where('id', $request['id'])->update($req_data);
        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }

    /**
     * 删除
     */
    public function dodel(Request $request)
    {
        $req_data = $request->only(['id']);
        $Validate = new ValidateRecovery('del');
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $req_data['is_del']                   = 2;
        $req_data['update_account_id']        = $accountinfo['id'];
        $req_data['update_account_real_name'] = $accountinfo['real_name'];
        $req_data['update_at']                = time();

        Recovery::where('id', $request['id'])->update($req_data);
        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }
}
